//
//  AddressPickerController.m
//  OnionHome
//
//  Created by fengjie on 2019/10/22.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "AddressPickerController.h"
#import "CZQAddressPicker.h"

@interface AddressPickerController ()<CZQAddressPickerDelegate>
/**<#属性#>*/
@property (nonatomic,strong)CZQAddressPicker *addressPicker;
/**<#属性#>*/
@property (nonatomic,strong)NSString*addStr;
@end

@implementation AddressPickerController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight - 216 -50, KScreenWidth, 50)];
    topView.backgroundColor = KWhiteColor;
    [self.view addSubview:topView];

    
    UIButton * cleanBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [cleanBtn setTitle:@"清除" forState:UIControlStateNormal];
    [cleanBtn addTarget:self action:@selector(cleanBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:cleanBtn];
    
    UIButton * sureBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [sureBtn setTitle:@"完成" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:sureBtn];
    
    [cleanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(44, 44));
    }];
    
    
    
    [self.view addSubview:self.addressPicker];

}

//消除
-(void)cleanBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}
//完成
-(void)sureBtnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    if (StrValid(self.addStr)) {
        BlockSafeRun(self.backBlock,self.addStr);
    }
}
- (void)addressPicker:(CZQAddressPicker *)addressPicker
  didSelectedProvince:(CZQProvince *)province
                 city:(CZQCity *)city
             district:(CZQDistrict *)district
               pIndex:(NSInteger)pIndex
               cIndex:(NSInteger)cIndex
               dIndex:(NSInteger)dIndex {
    DLog(@"\n省:%@ index:%ld\n市:%@ index:%ld\n区:%@ index:%ld ",province.n, (long)pIndex, city.n, (long)cIndex, district.n, (long)dIndex);
    self.addStr = [NSString stringWithFormat:@"%@ %@ %@",province.n,city.n,district.n];
}
-(CZQAddressPicker*)addressPicker{
    if(!_addressPicker){
        _addressPicker=[[CZQAddressPicker alloc] initWithFrame:CGRectMake(0, KScreenHeight - 216,KScreenWidth, 216)];
        _addressPicker.delegate = self;
        [_addressPicker selectedProvinceIndex:0 cityIndex:0 districtIndex:0];
    }
    return _addressPicker;
}
@end
